import unittest
import HTMLTestRunner
import os, sys, getopt
from datetime import datetime
from testcases.testmonkey import TestMonkey
from monkeytestcase import MonkeyTestCase
from uploadresult import UploadResult
from devicectrl import DeviceCtrl,KeyCodes
from logcat_run import logcats

'''测试任务类'''
class Task():
    def __init__(self, deviceid, apkversion, package, events, throttle, seed, reportfile):
        self.deviceid = deviceid
        self.devctrl =  DeviceCtrl(deviceid)
        self.report_title = '质量保障部 Android极速版Monkey测试报告'
        self.package = package
        self.events = events
        self.throttle = throttle
        self.seed = seed
        self.reportfile = reportfile
        self.apkversion = apkversion
    
    def wake(self):
        '''点亮并解锁'''
        #自动化点亮并滑动解锁
        self.devctrl.wakeup()
      
    def start(self):
        '''开始测试'''
       
        suite = unittest.TestSuite()
        print ('开始加载测试用例')
        #taskcmd = '  -p com.example.monkey_test -p com.ennova.standard  --pct-anyevent 20 --pct-touch 40 --pct-motion 30 --ignore-crashes --ignore-timeouts --monitor-native-crashes  --throttle %s -s %s  -v-v-v %s ' % (self.throttle, self.seed ,self.events)
        taskcmd = '-p com.che.truckhome.speed  --pct-anyevent 20 --pct-touch 40 --pct-motion 30 --ignore-crashes ' \
                  '--ignore-timeouts --monitor-native-crashes  --throttle %s -s %s  -v-v-v %s ' % \
                  (self.throttle, self.seed ,self.events)
        self.report_des = '模块版本:'+ self.apkversion + ' 命令:' + taskcmd

        suite.addTest(MonkeyTestCase.loadtestcase(TestMonkey, monkeycmd=taskcmd, deviceid=self.deviceid, story='测试monkey1'))

        #唤醒
        self.wake()
        print('monkey参数配置: ' + taskcmd )
        print('请等待执行完成......')

        #开始执行
        with open(self.reportfile, 'wb') as f:
            return HTMLTestRunner.HTMLTestRunner(stream=f, title=self.report_title, 
                    description=self.report_des, verbosity=2).run(suite)



          
